package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysRole;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysMenuService;
import com.zuipin.service.ISysResourceService;
import com.zuipin.service.ISysRoleService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.SysRolePermissionVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 角色Controller
 * 
 * @ClassName: SysRoleController
 * @Description: TODO
 * @author robot
 * @date 2017年09月147日 13:40:12
 */
@Api(value = "系统 角色信息-zhuangjiajian", description = "角色")
@RestController
@RequestMapping(value = "/sysRole")
public class SysRoleController {
	@Resource
	public ISysRoleService		sysRoleService;
	@Resource
	public ISysMenuService		sysMenuService;
	@Resource
	public ISysResourceService	sysResourceService;
	
	@RequestMapping(value = "/seachRoleInfo", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-搜索角色 信息并分页", notes = "根据编号名称和状态进行查询")
	@Valids({})
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysRole.roleName", value = "角色名称模糊查询", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.roleStatus", value = "是否冻结 0:未冰结 1已冻结", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.roleNo", value = "角色编号JS0001 模糊查询", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<SysRole>> seachRoleInfo(@Inject @ApiIgnore SysRole sysRole, @Inject @ApiIgnore Pagination page) {
		PageResult<List<SysRole>> result = new PageResult<List<SysRole>>();
		result.setData(sysRoleService.seachSysRole(sysRole, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/addSysRole", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-新增角色", notes = "编号未填写则自动生成、名称必填项<p>描述：新增角色接口<br />返回码<br />0-成功<br />1003-数据库异常<br />3005-角色编码重复<br /></p>")
	@Valids({ @Valid(name = "sysRole.roleName", maxLength = 50), @Valid(name = "sysRole.remark", maxLength = 100) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysRole.roleName", value = "角色名称", required = true, dataType = "roleName", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.remark", value = "角色描述", dataType = "remark", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.createId", required = true, value = "创建人", dataType = "updateId", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.roleNo", value = "默认角色编号为：JS+4位流水号，流水号值=ID", required = false, dataType = "roleNo", paramType = "query"), })
	public Result<SysRole> addSysRole(@Inject @ApiIgnore SysRole sysRole) {
		return sysRoleService.addSysRole(sysRole);
	}
	
	@RequestMapping(value = "/updateRole", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-根据主键修改角色", notes = "修改角色<p>描述：新增角色接口<br />返回码<br />0-成功<br />1003-数据库异常<br />3005-角色编码重复<br /></p>")
	@Valids({ @Valid(name = "sysRole.id", maxLength = 19), @Valid(name = "sysRole.roleName", maxLength = 100), @Valid(name = "sysRole.delFlag", maxLength = 3),
			@Valid(name = "sysRole.roleStatus", maxLength = 3), @Valid(name = "sysRole.sort", maxLength = 10), @Valid(name = "sysRole.remark", maxLength = 100),
			@Valid(name = "sysRole.createId", maxLength = 10), @Valid(name = "sysRole.createTime", maxLength = 19), @Valid(name = "sysRole.updateId", maxLength = 10),
			@Valid(name = "sysRole.updateTime", maxLength = 19), @Valid(name = "sysRole.roleNo", maxLength = 20), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysRole.id", value = "主键 自增长", required = true, dataType = "id", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.roleName", value = "角色名称", required = true, dataType = "roleName", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.remark", value = "角色描述", dataType = "remark", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.updateId", value = "更新人", required = true, dataType = "updateId", paramType = "query"),
			@ApiImplicitParam(name = "sysRole.roleNo", value = "默认角色编号为：JS+4位流水号，流水号值=ID", dataType = "roleNo", paramType = "query"), })
	public Result<SysRole> updateSysRole(@Inject @ApiIgnore SysRole sysRole) {
		return sysRoleService.update(sysRole);
	}
	
	/**
	 * 查看角色详情
	 * @date 2017-9-14 17:00:07
	 * @author zhuangjiajian
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getRoleDetail", method = RequestMethod.GET)
	@ApiOperation(value = "6期-zhuangjiajian-查看角色基本详情信息", notes = "角色详情")
	public Result<SysRole> getRoleDetail(@ApiParam(required = true, name = "id", value = "主键 自增长") @RequestParam Long id) {
		SysRole sysRole = sysRoleService.findById(id);
		Result<SysRole> result = new Result<SysRole>();
		if (sysRole != null) {
			result.setData(sysRole);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	/**
	 * 查看角色对应的权限详情信息
	 * （返回角色资源   角色菜单信息）
	 * @date 2017-9-14 17:00:07
	 * @author zhuangjiajian
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getRolePermissionDetail", method = RequestMethod.GET)
	@ApiOperation(value = "6期-zhuangjiajian-查看角色对应的权限详情信息", notes = "查看角色对应的权限详情信息 sysId=0时为通用的信息列表")
	public Result<List<SysRolePermissionVo>> getRolePermissionDetail(@ApiParam(required = true, name = "roleId", value = "主键 自增长") @RequestParam Long roleId) {
		
		return sysRoleService.getRolePermissionDetail(roleId);
		
	}
	
	@RequestMapping(value = "/modifyRoleStatus", method = RequestMethod.GET)
	@ApiOperation(value = "6期-zhuangjiajian-更改角色状态", notes = "更改角色状态")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "操作者ID", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "0|1   0启用 1禁用", dataType = "integer", paramType = "query") })
	public Result<SysRole> modifyRoleStatus(@RequestParam(required = true) Integer updateId, @RequestParam(required = true) Long id,
			@RequestParam(required = true) Integer status) {
		Result<SysRole> result = new Result<SysRole>();
		SysRole sysRole = sysRoleService.findById(id);
		String msg = "";
		if (sysRole.getRoleStatus().equals(SysRole.ENABLE)) {
			msg = "启用";
		} else {
			msg = "禁用";
		}
		if (sysRole.getRoleStatus().equals(status)) {
			result.setErrorCode(ErrorCode.MODITY_STATUS_FAIL);
			result.setMessage("状态已经" + msg + "不能重复" + msg);
			return result;
		}
		
		SysRole modifyRole = new SysRole();
		modifyRole.setId(id);
		modifyRole.setRoleStatus(status);
		modifyRole.setUpdateId(updateId);
		modifyRole.setUpdateTime(DateUtils.getCurrentDateTime());
		sysRoleService.update(modifyRole);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("角色状态已改变成功");
		return result;
	}
	
	@RequestMapping(value = "/del", method = RequestMethod.DELETE)
	@ApiOperation(value = "根据主键删除角色", notes = "删除角色")
	public Result<SysRole> delSysRole(Long id) {
		Result<SysRole> result = new Result<SysRole>();
		sysRoleService.delete(id);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
